using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
using YooAsset.Editor;
using HybridCLR.Editor.Commands;

namespace HybridCLR
{
    public static partial class BuildPlayerHelper
    {
        [MenuItem("HybridCLR/Build Player/Win64")]
        public static void Build_Player_Win64()
        {
            BuildTarget target = BuildTarget.StandaloneWindows64;
            BuildTarget activeTarget = EditorUserBuildSettings.activeBuildTarget;
            if (activeTarget != BuildTarget.StandaloneWindows64 && activeTarget != BuildTarget.StandaloneWindows)
            {
                Debug.LogError("请先切到Win平台再打包");
                return;
            }
            // Get filename.
            string outputPath = $"{BuildConfig.ProjectDir}/Release-Win64";

            var buildOptions = BuildOptions.Development;

            string location = GetPackLocation(outputPath);

            Debug.Log("====> Build App");
            BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions()
            {
                scenes = new string[] { "Assets/ResourcesHot/Scenes/0.Init.unity" },
                locationPathName = location,
                options = buildOptions,
                target = target,
                targetGroup = BuildTargetGroup.Standalone,
            };

            //Debug.Log("====> 第1次 Build App(为了生成补充AOT元数据dll)");
            //BuildPipeline.BuildPlayer(buildPlayerOptions);

            Debug.Log("====> 编译Asset Bundle");
            CompileDllCommand.CompileDllWin64();

            // 拷贝dll到热更新目录
            CopyAssetBundles(target);

            AssetBundleBuilderWindow.ExecuteBuildInternal(target);

            Debug.Log("====> 第2次打包");

            var report = BuildPipeline.BuildPlayer(buildPlayerOptions);
            if (report.summary.result != UnityEditor.Build.Reporting.BuildResult.Succeeded)
            {
                Debug.LogError("====> 打包失败");
                return;
            }

#if UNITY_EDITOR
            Application.OpenURL($"file:///{outputPath}");
#endif
        }

        [MenuItem("HybridCLR/Build Player/Android64")]
        public static void Build_Player_Android64()
        {
            BuildTarget target = BuildTarget.Android;
            BuildTarget activeTarget = EditorUserBuildSettings.activeBuildTarget;
            if (activeTarget != BuildTarget.Android)
            {
                Debug.LogError("请先切到Android平台再打包");
                return;
            }
            // Get filename.
            string outputPath = $"{BuildConfig.ProjectDir}/Release-Android";

            var buildOptions = BuildOptions.None;

            string location = GetPackLocation(outputPath);

            Debug.Log("====> Build App");
            BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions()
            {
                scenes = new string[] { "Assets/ResourcesHot/Scenes/0.Init.unity" },
                locationPathName = location,
                options = buildOptions,
                target = target,
                targetGroup = BuildTargetGroup.Android,
            };

            //Debug.Log("====> 第1次 Build App(为了生成补充AOT元数据dll)");
            //BuildPipeline.BuildPlayer(buildPlayerOptions);

            Debug.Log("====> 编译Asset Bundle");
            CompileDllCommand.CompileDllAndroid();

            // 拷贝dll到热更新目录
            CopyAssetBundles(target);

            AssetBundleBuilderWindow.ExecuteBuildInternal(target);

            Debug.Log("====> 第2次打包");

            var report = BuildPipeline.BuildPlayer(buildPlayerOptions);
            if (report.summary.result != UnityEditor.Build.Reporting.BuildResult.Succeeded)
            {
                Debug.LogError("====> 打包失败");
                return;
            }

#if UNITY_EDITOR
            Application.OpenURL($"file:///{outputPath}");
#endif
        }

        [MenuItem("HybridCLR/Build Hotfix/Win64")]
        public static void Build_Hotfix_Win64()
        {
            BuildTarget target = BuildTarget.StandaloneWindows64;

            Debug.Log("====> 编译Asset Bundle");
            CompileDllCommand.CompileDllWin64();

            // 拷贝dll到热更新目录
            CopyAssetBundles(target);

            AssetBundleBuilderWindow.ExecuteBuildInternal(target);
        }

        [MenuItem("HybridCLR/Build Hotfix/Android64")]
        public static void Build_Hotfix_Android64()
        {
            BuildTarget target = BuildTarget.Android;

            Debug.Log("====> 编译Asset Bundle");
            CompileDllCommand.CompileDllAndroid();

            // 拷贝dll到热更新目录
            CopyAssetBundles(target);

            AssetBundleBuilderWindow.ExecuteBuildInternal(target);
        }
    }
}
